$("button.login").click(function() {
    Util.jumpUrl({
        file: 'login.html',
        try: 0,
    });
});
$("button.register").click(function() {
    Util.jumpUrl({
        file: 'register.html',
        try: 0,
    });
});

// 是否处于维护状态
Classify.queryExt("settings", "mainten", true, {}, function(v) {
    if (v.close) {
        Util.jumpUrl({
            file: "maintain.html"
        });
    }
});

$(".footer .gamehall").click(function() {
    if (!logined) alert2("请先登录！");
    else
        Util.jumpUrl({
            file: 'center.html',
            cmd: 'gamecenter',
        });
});
$(".footer .money").click(function() {
    if (!logined) 
        Util.jumpUrl({
            file: "login.html",
        });
    else if (urlInfo.params.try == 1) alert2("试玩账号无权访问，请先注册。");
    else
        Util.jumpUrl({
            file: 'center.html',
            cmd: 'charge',
            back: encodeURIComponent(window.location.href),
        });
});
$(".footer .me").click(function() {
    if (!logined) 
        Util.jumpUrl({
            file: "login.html",
        });
    else
        Util.jumpUrl({
            file: 'center.html',
            cmd: 'index',
        });
});

// 网站名称
Classify.query("settings", "title", false, "中盈", function(v) {
    $("title").html("" + v);
    $(".copyright span").html(v);
});

// 网站LOGO
if (urlInfo.params.bocai == 1) {
    $(".logo img").attr("src", "");
    $("li.pc").hide();
}
else {
    Classify.queryExt("settings", "logo-big", false, "", function(v) {
        if (v) {
            var url = context.config.url.replace(":9090", "");
            if (urlInfo.params.try == 1) var url = context.config.tryUrl.replace(":9090", "");
            $(".logo img").attr("src", url + v);
        }
    });
}

// 公告
Classify.queryExt("settings", "board", true, "[]", function(v) {
    var m = $("#marquee"); m.empty();
    for (var i in v) {
        var d = v[i];
        var li = $("<li></li>");
        li.append(d.content);
        m.append(li);
    }

    // 实现公告的滚动效果
    $(document).ready(function() {
        if ($("#marquee").marquee)
            $("#marquee").marquee({
                pauseSpeed: 4000,
                scrollSpeed: 30,
                showSpeed: 250,
            });
    });
});

// 广告图片
Classify.queryExt("settings", "ads_phone", true, [], function(ads) {
    $(function() {
        var index = 0;
        for (var i = 1; i <= 5; i++) {
            if (!ads[i]) continue;
            var url = context.config.url.replace(":9090", "");
            if (urlInfo.params.try == 1) url = context.config.tryUrl.replace(":9090", "");
            $("#demo-slider").flexslider('addSlide', 
                '<li><img src="' + url + ads[i] + '"/></li>', index++);
        }
        $("#demo-slider").flexslider("removeSlide", index);
        _mainScroll.refresh();
        setInterval(function() {
            _mainScroll.refresh();
        }, 1500);
    });
});

// 检查下用户是否已经处于登录状态
var logined = false;
function queryTimer() {
    ME.query(function(r) {
        if (r) {
            // 处于登录状态
            logined = true;
            setTimeout(queryTimer, 10*1000);
        }
        else {
            logined = false;
        }

        // 刷新下界面
        Login.redraw();
    });
}
queryTimer();

$("#user-info .center").click(function() {
    // 跳转到个人中心
    Util.jumpUrl({
        file: 'center.html',
        cmd: 'index',
    });
});
$("button.logout").click(function() {
    confirm2("确认要退出该账号？", function() {
        ME.logout();
        window.location.href = "login.html";
    });
});
$("button.gamehall").click(function() {
    if (!logined) {
        Util.jumpUrl({
            file: "login.html",
        })
        return;
    }
    Util.jumpUrl({
        file: 'center.html',
        cmd: 'gamecenter',
    });
});

$("button.money").click(function() {
    if (!logined) {
        Util.jumpUrl({
            file: "login.html",
        })
        return;
    }
    Util.jumpUrl({
        file: 'center.html',
        cmd: 'charge',
        back: encodeURIComponent(window.location.href),
    });
});
$(".hot-games li").click(function(e) {
    var game = $(e.currentTarget).attr("class");
    if (game == "more") {
        if (!logined) {
            Util.jumpUrl({
                file: "login.html",
            })
            return;
        }
        Util.jumpUrl({
            file: 'center.html',
            cmd: 'gamecenter',
        });
        return;
    }
    if (game == "kefu") {
        _MEIQIA('showPanel');
        return;
    }
    if (game == "kefu" || game == "try" || game == "pc" || game == "app-download") return;

    if (!logined) {
        Util.jumpUrl({
            file: "login.html",
        })
        return;
    }
    Util.jumpUrl({
        file: 'main2.html',
        game: game,
    });
});
